﻿#If NETFX_CORE Then

Imports Windows.Storage

Namespace Global.System.Runtime.InteropServices.WindowsRuntime

    Public Module Extension

        <Extension()> _
        Public Function LoadFromStream(ByVal [Me] As WriteableBitmap, ByVal s As Stream) As Boolean
            Try
                Using pixelStream = [Me].PixelBuffer.AsStream()
                    pixelStream.Seek(0, SeekOrigin.Begin)
                    s.CopyTo(pixelStream)
                End Using
                [Me].Invalidate()
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        <Extension()> _
        Public Function FileExists(ByVal [Me] As StorageFolder, ByVal fileName As String) As Boolean
            Dim items = [Me].GetFilesAsync().GetResults()
            Return (From item In items Where item.Name.ToLower() = fileName.ToLower() Select item).Count() = 1
        End Function

        <Extension()> _
        Public Function DirectoryExists(ByVal [Me] As StorageFolder, ByVal dirName As String) As Boolean
            Dim items = [Me].GetFoldersAsync().AsTask().Result
            Return (From item In items Where item.Name.ToLower() = dirName.ToLower() Select item).Count() = 1
        End Function

        <Extension()> _
        Public Sub DeleteDirectory(ByVal [Me] As StorageFolder, ByVal fileName As String)
            [Me].GetFileAsync(fileName).GetResults().DeleteAsync().GetResults()
        End Sub

        <Extension()> _
        Public Sub CreateDirectory(ByVal [Me] As StorageFolder, ByVal dirName As String)
            [Me].CreateFolderAsync(dirName).GetResults()
        End Sub

        <Extension()> _
        Public Function GetFileNames(ByVal [Me] As StorageFolder, ByVal filter As String) As String()
            Return (From item In [Me].GetFilesAsync().GetResults() Select item.Name).ToArray()
        End Function

    End Module

End Namespace

#End If